/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.ims.modules.momarketing.entity.MicroProduct;
import com.boxin.ims.modules.momarketing.entity.MicroProductCategory;
import com.boxin.ims.modules.momarketing.entity.Project;
import com.boxin.ims.modules.momarketing.service.MicroProductCategoryService;
import com.boxin.ims.modules.momarketing.service.MicroProductService;
import com.boxin.ims.modules.momarketing.service.ProjectService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 微网站产品Controller
 * @author Jakemanse
 * @version 2013-11-25
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/momarketing/microProduct")
public class MicroProductController extends BaseController {

	@Autowired
	private MicroProductService microProductService;
	
	@Autowired
	private MicroProductCategoryService productCategoryService;
	
	@Autowired
	private ProjectService projectService;
	
	@ModelAttribute
	public MicroProduct get(@RequestParam(required=false) Long id) {
		if (id != null){
			return microProductService.get(id);
		}else{
			return new MicroProduct();
		}
	}
	
	@RequiresPermissions("momarketing:microProduct:view")
	@RequestMapping(value = {"list", ""})
	public String list(MicroProduct microProduct, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		
		
		if (!user.isAdmin()){
			microProduct.setUser(user);
		}
		
		
		
		
        Page<MicroProduct> page = microProductService.find(new Page<MicroProduct>(request, response), microProduct); 
        model.addAttribute("page", page);
		return "modules/momarketing/microProductList";
	}

	@RequiresPermissions("momarketing:microProduct:view")
	@RequestMapping(value = "form")
	public String form(MicroProduct microProduct, Model model,RedirectAttributes redirectAttributes) {
	
		User user = UserUtils.getUser();
		if (microProduct.getUser() == null || microProduct.getUser().getId() <1){
			microProduct.setUser(user);
		}
		
		Project project  = projectService.getByUserId(user.getId());
		if(project!=null){
			microProduct.setProject(project);
			
			List<MicroProductCategory> categoryList = 	productCategoryService.findCategoryListByProjectId(project.getId());
			model.addAttribute("categoryList", categoryList);
			model.addAttribute("project", project);
			if(categoryList==null || categoryList.isEmpty()){
				
				addMessage(redirectAttributes, "添加产品前,请先添加产品分类");
				return "modules/momarketing/microProductForm"; 
			}
		}else{
			addMessage(redirectAttributes, "请先添加创建微网站");
			return "modules/momarketing/microProductForm"; 
		}
		
		
		
		model.addAttribute("microProduct", microProduct);
		return "modules/momarketing/microProductForm";
	}

	@RequiresPermissions("momarketing:microProduct:edit")
	@RequestMapping(value = "save")
	public String save(MicroProduct microProduct, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, microProduct)){
			return form(microProduct, model,redirectAttributes);
		}
		
		User user = UserUtils.getUser();
		Project project  = projectService.getByUserId(user.getId());
		//判断是否已创建微网站
		if(project!=null){
			
			MicroProductCategory category = microProduct.getCategory();
			if(category==null || category.getId()<0){
				addMessage(redirectAttributes, "添加产品前,请先添加产品分类");
				return form(microProduct, model,redirectAttributes);
			}
		}else{
			return form(microProduct, model,redirectAttributes);
		}
		
		
		microProductService.save(microProduct);
		addMessage(redirectAttributes, "保存微网站产品'" + microProduct.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/momarketing/microProduct/?repage";
	}
	
	@RequiresPermissions("momarketing:microProduct:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		microProductService.delete(id);
		addMessage(redirectAttributes, "删除微网站产品成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/momarketing/microProduct/?repage";
	}

}
